package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Random;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import conference.Author;
import conference.Conference;
import conference.Manuscript;
import conference.Status;

/**
 * A gui for the PC Views Manuscripts user story.
 * @author Dan Garrison
 * @version Fall 2010
 */
@SuppressWarnings("serial")
public class ViewManuscriptPanel extends Observable {
	private static final String TITLE = "Manuscript Evaluation";
	private static final int PREF_BUTTON_WIDTH = 300;
	private static final int PREF_PANEL_HEIGHT = 500;
	private static final int PREF_CONTROL_HEIGHT = 25;
	private static final int TITLE_WIDTH = 500;
	private static final int AUTHOR_WIDTH = 200;
	private static final int REVIEW_WIDTH = 100;
	private static final int STATUS_WIDTH = 100;
	private static final int PREF_NAME_WIDTH = 100;
	private static final String SPACER = "    ";
	private static final String[] COLUMN_NAMES = {
		"Title",
		"Author",
		"Reviews",
		"Status"
	};
	private Dimension myDim;
	private JPanel myPanel;
	private Conference myConference;
	private Manuscript[] workingManuscripts;
	private Manuscript myViewedManuscript;
	
	/**
	 * Constructor that requires a predefined conference
	 * @param the_conference that has been selected by the user
	 */
	public ViewManuscriptPanel(Conference the_conference){
		myPanel = new JPanel(new BorderLayout());
		myConference = the_conference;
		Toolkit tk = Toolkit.getDefaultToolkit();
		myDim = tk.getScreenSize();
		workingManuscripts = myConference.getAllManuscripts();
		initPanel();
	}
	
	/**
	 * Constructor that requires an XML file name and conference index
	 * @param the_file_name An XML file that has conferences stored
	 * @param the_conference_index The index of the conference in the XML file
	 */
	public ViewManuscriptPanel(String the_file_name, int the_conference_index){
		myPanel = new JPanel(new BorderLayout());
		myConference = ConsumeXMLData.extractConferences(the_file_name, false).get(the_conference_index);
		workingManuscripts = myConference.getAllManuscripts();
		Toolkit tk = Toolkit.getDefaultToolkit();
		myDim = tk.getScreenSize();
		initPanel();
	}
	
	/**
	 * Sets up the containers for the gui.
	 */
	private void initPanel() {
		myPanel.removeAll();
		JPanel headerPanel = 
			new JPanel(new GridLayout(1, 4));
		JPanel lowerPanel = 
			new JPanel(new GridLayout(1, 4));
		JPanel titlePanelTop = new JPanel();
		JPanel authorPanelTop = new JPanel();
		JPanel reviewPanelTop = new JPanel();
		JPanel statusPanelTop = new JPanel();
		JPanel titlePanel = new JPanel();
		JPanel authorPanel = new JPanel();
		JPanel reviewPanel = new JPanel();
		JPanel statusPanel = new JPanel();
		JPanel buttonPanel = new JPanel();
		
		buttonPanel.add(GuiController.
				getSwitchConferenceButton());
		buttonPanel.add(GuiController.
				getSwitchUserButton());
		buttonPanel.add(GuiController.getSaveButton());
		buttonPanel.add(GuiController.getQuitButton());
		myPanel.add(buttonPanel, BorderLayout.SOUTH);
		
		setTitlePanelTop(titlePanelTop);
		setAuthorPanelTop(authorPanelTop);
		setReviewPanelTop(reviewPanelTop);
		setStatusPanelTop(statusPanelTop);
		
		setUpTitlePanel(titlePanel);
		setUpAuthorPanel(authorPanel);
		setUpReviewPanel(reviewPanel);
		setUpStatusPanel(statusPanel);
		
		titlePanel.setLayout(new BoxLayout(titlePanel, 
				BoxLayout.Y_AXIS));
		authorPanel.setLayout(new BoxLayout(authorPanel, 
				BoxLayout.Y_AXIS));
		reviewPanel.setLayout(new BoxLayout(reviewPanel, 
				BoxLayout.Y_AXIS));
		statusPanel.setLayout(new BoxLayout(statusPanel, 
				BoxLayout.Y_AXIS));
		
		titlePanel.setPreferredSize(
				new Dimension(TITLE_WIDTH, 
						PREF_PANEL_HEIGHT));
		authorPanel.setPreferredSize(
				new Dimension(AUTHOR_WIDTH, 
						PREF_PANEL_HEIGHT));
		reviewPanel.setPreferredSize(
				new Dimension(REVIEW_WIDTH, 
						PREF_PANEL_HEIGHT));
		statusPanel.setPreferredSize(
				new Dimension(STATUS_WIDTH, 
						PREF_PANEL_HEIGHT));
		
		titlePanel.setMaximumSize(
				new Dimension(TITLE_WIDTH, 
						PREF_PANEL_HEIGHT));
		authorPanel.setMaximumSize(
				new Dimension(AUTHOR_WIDTH, 
						PREF_PANEL_HEIGHT));
		reviewPanel.setMaximumSize(
				new Dimension(REVIEW_WIDTH, 
						PREF_PANEL_HEIGHT));
		statusPanel.setMaximumSize(
				new Dimension(STATUS_WIDTH, 
						PREF_PANEL_HEIGHT));
		
		titlePanelTop.setLayout(new BoxLayout(
				titlePanelTop, 
				BoxLayout.Y_AXIS));
		authorPanelTop.setLayout(new BoxLayout(
				authorPanelTop, 
				BoxLayout.Y_AXIS));
		reviewPanelTop.setLayout(new BoxLayout(
				reviewPanelTop, 
				BoxLayout.Y_AXIS));
		statusPanelTop.setLayout(new BoxLayout(
				statusPanelTop, 
				BoxLayout.Y_AXIS));
		
		titlePanelTop.setPreferredSize(
				new Dimension(TITLE_WIDTH, 
						PREF_CONTROL_HEIGHT));
		authorPanelTop.setPreferredSize(
				new Dimension(AUTHOR_WIDTH, 
						PREF_CONTROL_HEIGHT));
		reviewPanelTop.setPreferredSize(
				new Dimension(REVIEW_WIDTH, 
						PREF_CONTROL_HEIGHT));
		statusPanelTop.setPreferredSize(
				new Dimension(STATUS_WIDTH, 
						PREF_CONTROL_HEIGHT));
		
		titlePanelTop.setMaximumSize(
				new Dimension(TITLE_WIDTH, myDim.height));
		authorPanelTop.setMaximumSize(
				new Dimension(AUTHOR_WIDTH, myDim.height));
		reviewPanelTop.setMaximumSize(
				new Dimension(REVIEW_WIDTH, myDim.height));
		statusPanelTop.setMaximumSize(
				new Dimension(STATUS_WIDTH, myDim.height));

		headerPanel.setLayout(new BoxLayout(headerPanel, 
				BoxLayout.X_AXIS));
		lowerPanel.setLayout(new BoxLayout(lowerPanel, 
				BoxLayout.X_AXIS));
		
		headerPanel.add(titlePanelTop);
		headerPanel.add(authorPanelTop);
		headerPanel.add(reviewPanelTop);
		headerPanel.add(statusPanelTop);
		
		lowerPanel.add(titlePanel);
		lowerPanel.add(authorPanel);
		lowerPanel.add(reviewPanel);
		lowerPanel.add(statusPanel);
		
		JScrollPane scrollPane = new JScrollPane(
				lowerPanel);
		scrollPane.getHorizontalScrollBar().
		setUnitIncrement(25);
		scrollPane.getVerticalScrollBar().
		setUnitIncrement(25);
		
		myPanel.add(headerPanel, BorderLayout.NORTH);
		myPanel.add(scrollPane, BorderLayout.CENTER);
	
		myPanel.add(headerPanel, BorderLayout.NORTH);
		myPanel.add(scrollPane, BorderLayout.CENTER);
		myPanel.validate();
	}
	
	/**
	 * Actions taken when a sort is performed.
	 */
	private void resetAfterSort() {
		workingManuscripts = myConference.
		getAllManuscripts();
		initPanel();
	}
	
	/**
	 * Sets up the header for the title panel.
	 * @param title a panel to put components in.
	 */
	private void setTitlePanelTop(JPanel title) {
		title.removeAll();
		JButton button = new JButton(
				new AbstractAction(COLUMN_NAMES[0]) {
			@Override
			public void actionPerformed(ActionEvent e) {
				myConference.sortByTitle();
				resetAfterSort();
			}
		});
		button.setMinimumSize(
				new Dimension(PREF_BUTTON_WIDTH, 
						PREF_CONTROL_HEIGHT));
		button.setMaximumSize(new Dimension(myDim.width, myDim.height));
		title.add(button);
	}
	
	/**
	 * Sets up the header for the author panel.
	 * @param author a panel to put components in.
	 */
	private void setAuthorPanelTop(JPanel author) {
		author.removeAll();
		JButton button = new JButton(
				new AbstractAction(COLUMN_NAMES[1]) {
			@Override
			public void actionPerformed(ActionEvent e) {
				myConference.sortByAuthor();
				resetAfterSort();
			}
		});
		button.setMinimumSize(
				new Dimension(PREF_BUTTON_WIDTH, 
						PREF_CONTROL_HEIGHT));
		button.setMaximumSize(
				new Dimension(myDim.width, 
						myDim.height));		
		author.add(button);
	}
	
	/**
	 * Sets up the header for the review panel.
	 * @param review a panel to put components in.
	 */
	private void setReviewPanelTop(JPanel review) {
		review.removeAll();
		JButton button = new JButton(
				new AbstractAction(COLUMN_NAMES[2]) {
			@Override
			public void actionPerformed(ActionEvent e) {
				myConference.sortByReviewsComplete();
				resetAfterSort();
			}
		});
		button.setMinimumSize(
				new Dimension(PREF_BUTTON_WIDTH, 
						PREF_CONTROL_HEIGHT));
		button.setMaximumSize(new Dimension(myDim.width, 
				myDim.height));		
		review.add(button);
	}
	
	/**
	 * Sets up the header for the status panel.
	 * @param status a panel to put components in.
	 */
	private void setStatusPanelTop(JPanel status) {
		status.removeAll();
		JButton button = new JButton(
				new AbstractAction(COLUMN_NAMES[3]) {
			@Override
			public void actionPerformed(ActionEvent e) {
				myConference.sortByStatus();
				resetAfterSort();
			}
		});
		button.setMinimumSize(
				new Dimension(PREF_BUTTON_WIDTH,
						PREF_CONTROL_HEIGHT));
		button.setMaximumSize(
				new Dimension(myDim.width, myDim.height));	
		status.add(button);
	}
	
	/**
	 * Sets up the for the title panel.
	 * @param title a panel to put components in.
	 */
	private void setUpTitlePanel(JPanel title) {
		title.removeAll();
		for (final Manuscript m : workingManuscripts) {
			System.out.println(m.getTitle());
			JButton button = new JButton(
					new AbstractAction() {
				@Override
				public void actionPerformed(
						ActionEvent e) {
					myViewedManuscript = m;
					setChanged();
					notifyObservers(State.VIEW_MANUSCRIPT);
				}
			});
			button.setMinimumSize(
					new Dimension(PREF_BUTTON_WIDTH,25));
			button.setMaximumSize(
					new Dimension(myDim.width,25));
			button.setLayout(new BorderLayout());
			JLabel label = new JLabel(m.getTitle());
			button.add(label, BorderLayout.WEST);
			title.add(button);
		}
	}
	
	/**
	 * Sets up the for the author panel.
	 * @param author a panel to put components in.
	 */
	private void setUpAuthorPanel(JPanel author) {
		author.removeAll();
		for (Manuscript m : workingManuscripts) {
			JLabel label = new JLabel(SPACER + 
					m.getAuthor().
					getLastName() + ", " + 
					m.getAuthor().getFirstName());
			label.setMinimumSize(
					new Dimension(PREF_NAME_WIDTH, 
					PREF_CONTROL_HEIGHT));
			label.setMaximumSize(
					new Dimension(PREF_NAME_WIDTH * 2, 
					PREF_CONTROL_HEIGHT));
			author.add(label);
		}
	}
	
	/**
	 * Sets up the for the review panel.
	 * @param review a panel to put components in.
	 */
	private void setUpReviewPanel(JPanel review) {
		review.removeAll();
		for (Manuscript m : workingManuscripts) {
			JLabel label = new JLabel(SPACER + 
					m.getNumberOfCompletedReviews() +
					" / " + 
					m.getNumberOfReviews());
			label.setMinimumSize(
					new Dimension(PREF_NAME_WIDTH, 
					PREF_CONTROL_HEIGHT));
			label.setMaximumSize(
					new Dimension(PREF_NAME_WIDTH * 2, 
					PREF_CONTROL_HEIGHT));
			review.add(label);
		}
	}
	
	/**
	 * Sets up the for the status panel.
	 * @param status a panel to put components in.
	 */
	private void setUpStatusPanel(JPanel status) {
		status.removeAll();
		for (Manuscript m : workingManuscripts) {
			JLabel label = new JLabel(SPACER + 
					m.getStatus());
			label.setMinimumSize(
					new Dimension(PREF_NAME_WIDTH, 
					PREF_CONTROL_HEIGHT));
			label.setMaximumSize(
					new Dimension(PREF_NAME_WIDTH * 2, 
					PREF_CONTROL_HEIGHT));
			status.add(label);
		}
	}
	
	/**
	 * @return the manuscript last selected to be viewed.
	 */
	public Manuscript getViewedManuscript() {
		return myViewedManuscript;
	}
	
	public JPanel getPanel() {
		return myPanel;
	}
	
	public String getTitle() {
		return TITLE;
	}
}

